import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',

  },
  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "goods" */ '@/views/HomeView.vue')
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "goods" */ '@/views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    redirect: '/user/cart',
    component: () => import(/* webpackChunkName: "user" */ '@/views/User.vue'),
    children: [
      {
        path: '/user/cart',
        component: () => import(/* webpackChunkName: "Cart" */ '@/components/User/cart.vue'),
      },
      {
        path: '/user/userinfo',
        component: () => import(/* webpackChunkName: "userInfo" */ '@/components/User/userInfo.vue'),
      }
    ],
  },
  {
    path: '/order',

    name: 'order',
    component: () => import(/* webpackChunkName: "order" */ '@/views/Order.vue')
  }
  , {
    path: '/free',
    redirect: '/error',
    name: 'free',
    component: () => import(/* webpackChunkName: "free" */ '@/views/Free.vue')
  }
  , {
    path: '/details',
    name: 'details',
    component: () => import(/* webpackChunkName: "details" */ '@/views/Details.vue')
  },
  //404页面
  {
    path: '*',
    name: 'error',
    component: () => import(/* webpackChunkName: "error" */ '@/views/Error.vue')
  }

]


const router = new VueRouter({
  mode: 'history',
  routes
})

/* 全局路由拦截 */
//每次路由跳转都来执行
/* router.beforeEach((to, from, next) => {
  if (to.path == "/user") {
    let token = localStorage.getItem('x-auth-token')
    if (token) {
      next()
    } else {
      store.dispatch("ToastStatus/asyncChangeIsShowToast", {
        msg: "请先登录！",
        type: "warning"
      })
    }
    return
  }
  next()
}) */

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {

  return originalPush.call(this, location).catch(err => err)
}

export default router
